/**
 * Definition for singly-linked list.
 * struct ListNode {
 *     int val;
 *     ListNode *next;
 *     ListNode() : val(0), next(nullptr) {}
 *     ListNode(int x) : val(x), next(nullptr) {}
 *     ListNode(int x, ListNode *next) : val(x), next(next) {}
 * };
 */
 
        
 class Solution {
public:
    ListNode* mergeTwoLists(ListNode* list1, ListNode* list2) {
        ListNode* result = new ListNode(0);
        ListNode* current = result;
        while (list1 != nullptr && list2 != nullptr) 
        {
            ListNode** pp = (list1->val < list2->val) ? &list1 : &list2;
            current->next = *pp;
            current = current->next;
            *pp = (*pp)->next;
        }
        current->next = (list1 == nullptr) ? list2 : list1;

        ListNode* ans = result->next;
        delete result;
        return ans;
    }
};
 
 
 /*
     ListNode* mergeTwoLists(ListNode* l1, ListNode* l2) {
        ListNode* newNode = new ListNode();
        ListNode* per = newNode;
        while(l1 && l2){            
            per->next = l1->val < l2->val ? l1:l2;
            per = per->next;
            l1->val < l2->val ? l1 = l1->next: l2 = l2->next;
        }
        per->next = l1?l1:l2;
        return newNode->next;
    }

 
 
 */